﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Shapes;
using System.IO;
using Silvermoon.OpenGL;
using System.Drawing;
using Silvermoon.Controls.Base;
using Silvermoon.Interfaces;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Control to display an Image.
    /// </summary>
    public class PictureBox : ChromeControl, IColor
    {
        #region fields

        private ImageShape shape;

        #endregion
        #region ctor

        public PictureBox()
            : base()
        {
            shape = new ImageShape();
            Shapes.AddLast(shape);
        }
        #endregion
        #region props

        public Size ImageSize { get; set; }

        public bool UseTextColor
        {
            get { return shape.UseTextColor; }
            set
            {
                shape.UseTextColor = value;
                Invalidate(InvalidationFlag.Color);
            }
        }


        /// <summary>
        /// Gets or sets the image texture that holds an image.
        /// </summary>
        public Texture Image
        {
            get { return shape.Image; }
            set
            {
                if (shape.Image != value)
                {
                    shape.Image = value; Invalidate(InvalidationFlag.LayoutAppearance);
                }
            }
        }

        #endregion
        #region Methods


        protected override void OnLayout()
        {
            if (CheckFlag(InvalidationFlag.Size | InvalidationFlag.Layout))
            {
                Size size = ImageSize;
                if (size.IsEmpty) size = Size;
                Rectangle r = new Rectangle(0, 0, size.Width, size.Height);
                shape.Bounds = Margin.Centered.Layout(r, Size);
            }
            base.OnLayout();
        }

        #endregion
    }

    public class ImageControl : Control
    {
        private BackgroundShape shape;

        public ImageControl()
            : base()
        {
            shape = new BackgroundShape();
            Shapes.AddLast(shape);
        }

        public Texture Texture
        {
            get { return shape.Texture; }
            set
            {
                shape.Texture = value;
                Invalidate(InvalidationFlag.LayoutAppearance);
            }
        }

        public RenderStyle RenderStyle
        {
            get { return shape.Style; }
            set
            {
                shape.Style = value;
                Invalidate(InvalidationFlag.Appearance);
            }
        }

        public override Color Color
        {
            get { return shape.Color; }
            set
            {
                shape.Color = value;
                Invalidate(InvalidationFlag.Color);
            }
        }

        protected override void PreLayout()
        {
            base.PreLayout();
        }

        protected override void OnLayout()
        {
            if (CheckFlag(InvalidationFlag.Size))
            {
                shape.Bounds = Margin.Centered.Layout(shape.Bounds, Size);
                //shape.Bounds = Control.ScaledRect(0, 0, Width, Height);
            }
            base.OnLayout();
        }
    }
}
